// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import {
    L2EProtocol
} from "../proto/ELProtocol";
import { loadImage, readStorage } from "../utils/CommonUtils";

import {
    NotificationCenter
} from "../utils/NotificationCenter";

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Node)
    listCtrl: cc.Node = null;

    private rankData: any = null;

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        if (cc.sys.isNative) {
            jsb.reflection.callStaticMethod("com/bbl/pineapple/JniHelper", "showBannerAd", "()V");
        }
        this.node.on(cc.Node.EventType.TOUCH_END, this.checkClose, this);
        // NotificationCenter.Instance.on(L2EProtocol.l2e_refresh_rank, this.refreshList, this);
    }
    
    onDestroy() {
        if (cc.sys.isNative) {
            jsb.reflection.callStaticMethod("com/bbl/pineapple/JniHelper", "hideBannerAd", "()V");
        }
        this.node.off(cc.Node.EventType.TOUCH_END, this.checkClose, this);
        // NotificationCenter.Instance.off(L2EProtocol.l2e_refresh_rank, this.refreshList, this);
    }

    start () {
        
    }

    // update (dt) {}

    
    public refreshList(data: any) {

        this.rankData = data.rankListData;

        let scrollCtrl = this.listCtrl.getComponent('ListViewCtrl');
        if (scrollCtrl.getTotalCount() == 0) {
            scrollCtrl.updateData(this.rankData);
            scrollCtrl.initialize();
            scrollCtrl.toTop();
        }else{
            scrollCtrl.refreshData(this.rankData);
        }
        let myIndex = -1;
        let logicCenter = cc.find('LogicCenterNode').getComponent('LogicCenter');
        let userInfo = logicCenter.getUserInfo();
        let myPlayerId = userInfo.playerId;
        for (let index = 0; index < data.rankListData.length; index++) {
            const task = data.rankListData[index];
            if (task.playerId == myPlayerId) {
                myIndex = index+1;
            }
        }

        let icon = cc.find('im_bg/im_bg_me/im_icon', this.node);
        let label_name = cc.find('im_bg/im_bg_me/label_name', this.node);
        let label_score = cc.find('im_bg/im_bg_me/im_bg_count/label_count', this.node);
        let label_idx = cc.find('im_bg/im_bg_me/label_idx', this.node);
        let im_rank = cc.find('im_bg/im_bg_me/im_no', this.node);
        let bg_rank = cc.find('im_bg/im_bg_me/im_rank_bg', this.node);
        let label_rank = cc.find('im_bg/im_bg_me/im_rank_bg/label_rank', this.node);

        if (myIndex < 1) {
            label_idx.active = true;
            im_rank.active = false;
            bg_rank.active = false;
        } else if (myIndex <= 3) {
            label_idx.active = false;
            bg_rank.active = false;
            im_rank.active = true;
            cc.resources.load('image/paihang_no'+myIndex, cc.SpriteFrame, (err: Error, spriteFrame: cc.SpriteFrame) => {
                if (err) {
                    console.log(err);
                    return;
                }
                im_rank.getComponent(cc.Sprite).spriteFrame = spriteFrame;
            });
        } else {
            label_idx.active = false;
            bg_rank.active = true;
            im_rank.active = false;
            label_rank.getComponent(cc.Label).string = String(myIndex);
        }
        if (userInfo.headIcon != null && userInfo.headIcon.length > 11) {
            loadImage(userInfo.headIcon, (spriteFrame: cc.SpriteFrame) => {
                icon.getComponent(cc.Sprite).spriteFrame = spriteFrame;
            });            
        }

        let score = userInfo.tbScore;
        if (score == undefined || score == null) {
            score = readStorage('BestScore') || 0;
        }

        // if (this.rankType == 0) {
        //     score = userInfo.tgetCoin;
        // } else if (this.rankType == 1) {
        //     score = userInfo.tbScore;
        // } else if (this.rankType == 2) {
        //     score = userInfo.tbmScore;
        // } else {
        //     score = userInfo.tgetMoney;
        // }

        label_name.getComponent(cc.Label).string = userInfo.nickName;
        if (myIndex > 0) {
            label_score.getComponent(cc.Label).string = String(data.rankListData[myIndex-1].data);
        }else{
            label_score.getComponent(cc.Label).string = String(score);
        }
    }

    public checkClose(event: cc.Touch) {
        let imBg = this.node.getChildByName('im_bg');
        let touchPos = imBg.convertToNodeSpaceAR(event.getLocation());
        cc.log("touch:"+touchPos);
        if (Math.abs(touchPos.x) > Math.abs(imBg.getContentSize().width/2) ||
                touchPos.y > imBg.getContentSize().height ||
                touchPos.y < 0) {
            this.node.active = false;
            this.node.destroy();
        }
    }

    public clickClose() {
        this.node.active = false;
        this.node.destroy();
    }
}
